<?php
namespace app\admin\controller;

use app\admin\model\Taocan as tcModel;
use app\admin\validate\tcValidate;
use tool\Log;

class Taocan extends Base
{
    public function index()
    {
        if (request()->isAjax()) {
            $limit = input('param.limit');
            $keywords = input('param.keywords');
            $where = [];
            if (!empty($keywords)) {
                $where[] = ['hos_name', 'like', '%' . $keywords . '%'];
            }

            $tc = new tcModel();
            $list = $tc->getTaocan($limit, $where);
            if (0 == $list['code']) {
                return json(['code' => 0, 'msg' => 'ok', 'count' => $list['data']->total(), 'data' => $list['data']->all()]);
            }
            return json(['code' => 0, 'msg' => 'ok', 'count' => 0, 'data' => []]);
        }
        return $this->fetch();
    }
    // 添加
    public function add()
    {
        if (request()->isPost()) {

            $param = input('post.');

            $validate = new tcValidate();
            if (!$validate->check($param)) {
                return json(['code' => -1, 'data' => '', 'msg' => $validate->getError()]);
            }

            $tc = new tcModel();
            $res = $tc->addHos($param);

            Log::write("添加医院：" . $param['hos_name']);

            return json($res);
        }

        return $this->fetch();
    }
    // 编辑
    public function edit()
    {
        if (request()->isPost()) {

            $param = input('post.');

            $validate = new WebsiteValidate();
            if (!$validate->scene('edit')->check($param)) {
                return json(['code' => -1, 'data' => '', 'msg' => $validate->getError()]);
            }

            $tc = new tcModel();
            $res = $tc->editHos($param);

            Log::write("编辑医院：" . $param['hos_name']);

            return json($res);
        }

        $id = input('param.id');
        $tc = new tcModel();

        $this->assign([
            'item' => $tc->getHosById($id)['data'],
        ]);

        return $this->fetch('edit');
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function del()
    {
        if (request()->isAjax()) {
            $id = input('param.id');

            $tc = new tcModel();
            $res = $tc->delCate($id);

            Log::write("删除医院：" . $id);
            return json($res);
        }
    }

}